<template>
  <div class="right-container f1 flex-v flex-b g-card h-100">
    <div class="card-brand border flex nowrap">
      实名认证
      <span
        class="red"
      >依法律法规需采用实名制发送内容，请勿发布违法违规视频，一经发现做封号处理同时将证据提交相关部门处理。</span>
    </div>
    <div class="f1">
      <div v-if="type === '个人认证'">
        <el-form ref="pForm" :model="pForm" :rules="rules" label-width="140px">
          <el-form-item label="实名类型：" prop="type">
            <div class="flex align-center">
              <mao-radio
                v-for="item in radioList"
                :key="item.label"
                v-model="type"
                :value="item.value"
                :label="item.label"
              />
            </div>
          </el-form-item>
          <el-form-item label="负责人姓名：" prop="name">
            <el-input v-model="pForm.name" class="transparent" />
          </el-form-item>
          <el-form-item label="负责人身份证：" prop="idcard">
            <el-input v-model="pForm.idcard" class="transparent" />
          </el-form-item>
          <el-form-item label="身份证正面：" prop="file">
            <div class="rel" style="width: fit-content">
              <upload
                ref="personUpload"
                :auto="false"
                :param="pForm"
                action="/cloudcut-api/userinfo/identification"
                @getFile="(file) => getFile('pForm', 'file', file)"
                @getUrl="(base64) => (pFileUrl = base64)"
              >
                <img
                  v-if="!pForm.file"
                  src="@/assets/images/personal/upload-id.png"
                  alt=""
                  class="id-card"
                >
                <img v-else :src="pFileUrl" alt="" class="id-card">
                <i
                  v-if="pForm.file"
                  class="el-icon-error abs del-icon"
                  @click.stop="del('pForm', 'personUpload', 'file', 'pFileUrl')"
                />
              </upload>
            </div>
          </el-form-item>
        </el-form>
      </div>
      <div v-if="type === '企业认证'">
        <el-form ref="bForm" :model="bForm" :rules="rules" label-width="140px">
          <el-form-item label="实名类型：" prop="type">
            <div class="flex align-center">
              <mao-radio
                v-for="item in radioList"
                :key="item.label"
                v-model="type"
                :value="item.value"
                :label="item.label"
              />
            </div>
          </el-form-item>
          <div class="grid-2">
            <el-form-item label="企业名称：" prop="company">
              <el-input v-model="bForm.company" class="transparent" />
            </el-form-item>
            <el-form-item label="负责人姓名：" prop="name">
              <el-input v-model="bForm.name" class="transparent" />
            </el-form-item>
            <el-form-item label="信用代码：" prop="regnum">
              <el-input v-model="bForm.regnum" class="transparent" />
            </el-form-item>
            <el-form-item label="负责人身份证：" prop="idcard">
              <el-input v-model="bForm.idcard" class="transparent" />
            </el-form-item>
            <el-form-item label="营业执照：" prop="file">
              <div class="rel" style="width: fit-content">
                <upload
                  ref="businessUpload"
                  :auto="false"
                  :param="bForm"
                  :url="oss + '/cloudcut-api/userinfo/identificationCompany'"
                  @getFile="(file) => getFile('bForm', 'file', file)"
                  @getUrl="(base64) => (bFileUrl = base64)"
                >
                  <img
                    v-if="!bForm.file"
                    src="@/assets/images/personal/upload-id.png"
                    alt=""
                    class="id-card"
                  >
                  <img v-else :src="bFileUrl" alt="" class="id-card">
                  <i
                    v-if="bForm.file"
                    class="el-icon-error abs del-icon"
                    @click.stop="
                      del('ruleForm', 'businessUpload', 'file', 'bFileUrl')
                    "
                  />
                </upload>
              </div>
            </el-form-item>
            <el-form-item label="身份证正面：" prop="idfile">
              <div class="rel" style="width: fit-content">
                <upload
                  ref="biUpload"
                  :auto="false"
                  :param="bForm"
                  :url="oss + '/cloudcut-api/userinfo/identificationCompany'"
                  @getFile="(file) => getFile('bForm', 'idfile', file)"
                  @getUrl="(base64) => (biFileUrl = base64)"
                >
                  <img
                    v-if="!bForm.idfile"
                    src="@/assets/images/personal/upload-id.png"
                    alt=""
                    class="id-card"
                  >
                  <img v-else :src="biFileUrl" alt="" class="id-card">
                  <i
                    v-if="bForm.idfile"
                    class="el-icon-error abs del-icon"
                    @click.stop="
                      del('bForm', 'biUpload', 'idfile', 'biFileUrl')
                    "
                  />
                </upload>
              </div>
            </el-form-item>
          </div>
        </el-form>
      </div>
    </div>
    <div class="flex-c align-center">
      <el-button
        type="primary"
        size="small"
        @click="submit"
      >提交实名</el-button>
    </div>
  </div>
</template>
<script>
import MaoRadio from '@/components/MaoRadio'
import Upload from '@/components/Upload/Index.vue'
export default {
  name: 'PersonalSetting',
  components: { MaoRadio, Upload },
  data() {
    return {
      radioList: [
        { label: '个人认证', value: '个人认证' },
        { label: '企业认证', value: '企业认证' }
      ],
      type: '个人认证',
      pForm: {
        name: '',
        idcard: '',
        file: ''
      },
      bForm: {
        company: '',
        regnum: '',
        file: '',
        name: '',
        idcard: '',
        idfile: ''
      },
      rules: {},
      pFileUrl: '',
      bFileUrl: '',
      biFileUrl: ''
    }
  },
  computed: {
    oss() {
      return this.$oss
    }
  },
  methods: {
    handleChange(file) {
      if (file.raw) {
        this.ruleForm.idcard = URL.createObjectURL(file.raw)
      }
    },
    del(name, compName, dataName = 'file', dataName2 = 'pFileUrl') {
      this.$confirm(`确定移除当前图片吗？`).then(() => {
        this[name][dataName] = ''
        this[dataName2] = ''
        this.$refs[compName].clearFiles()
      })
    },
    getFile(formName = 'pForm', dataName = 'file', file) {
      this[formName][dataName] = file
    },
    submit() {
      if (this.type === '个人认证') {
        this.$refs.personUpload.submit()
      } else {
        this.$refs.businessUpload.submit()
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.red {
  font-size: 14px;
  font-weight: 400;
  color: rgba(204, 37, 37, 1);
  margin-left: 20px;
  position: relative;
  top: 3px;
}
.del-icon {
  color: rgba(204, 37, 37, 1);
  right: 0;
  top: 0;
}
.id-card {
  width: 290px;
  height: 100px;
  border-radius: 15px;
  cursor: pointer;
}
.sub {
  font-size: 14px;
  font-weight: 400;
  color: rgba(113, 128, 150, 1);
}
.right-container {
  box-sizing: border-box;
  .userinfo {
    margin-bottom: 30px;
  }
  .card-brand {
    margin-bottom: 26px;
  }
  .grid-2 {
    display: grid;
    grid-template-columns: 1fr 1fr;
    grid-column-gap: 60px;
  }
  .avatar {
    width: 88px;
    height: 88px;
    border-radius: 50%;
    margin-right: 24px;
  }
  .link {
    font-size: 20px;
    margin-left: 16px;
  }
  .btn-list {
    margin-top: 16px;
  }
  .transparent {
    width: 100%;
    max-width: 290px;
  }
}
</style>
